package com.zhr.rhdb.etl;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.text.DecimalFormat;

/**
 * 时序库获取数据的值
 *
 * @author Java Liu
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class PointValue {
    private String pointId;
    private Double value;
    private Integer status;
    private String updateTime;
    private Long updateTimeLong;

    public PointValue() {
        this.value = 0.0;
    }

    public String getPointId() {
        return pointId;
    }

    public void setPointId(String tagName) {
        this.pointId = tagName;
    }

    public void setTagName(String pointId) {
        this.pointId = pointId;
    }

    public Double getValue() {
        return value;
    }

    public double getFormatValue(DecimalFormat decimalFormat) {
        if (decimalFormat == null) {
            return this.value;
        }
        return Double.parseDouble(decimalFormat.format(this.value).replace(",",""));
    }

    public void setValue(Double value) {
        this.value = value;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public void setTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public Long getUpdateTimeLong() {
        if (this.updateTimeLong == null) {
            this.updateTimeLong = DateUtil.parseDateTime(this.updateTime).getTime();
        }
        return updateTimeLong;
    }

    public void setUpdateTimeLong(Long updateTimeLong) {
        this.updateTimeLong = updateTimeLong;
    }

    @Override
    public String toString() {
        return "PointValue{" +
                "pointId='" + pointId + '\'' +
                ", value=" + value +
                ", status=" + status +
                ", updateTime='" + updateTime + '\'' +
                ", updateTimeLong=" + this.getUpdateTimeLong() +
                '}';
    }

    public int compare(PointValue p) {
        Double meValue = (this.value instanceof Double) ? (Double) this.value
                : (Double.valueOf(String.valueOf(this.value)));
        Double dstValue = (p.getValue() instanceof Double) ? (Double) p.getValue()
                : (Double.valueOf(String.valueOf(p.getValue())));
        return meValue.compareTo(dstValue);
    }

    public PointValue copy() {
        PointValue newValue = new PointValue();
        newValue.setStatus(this.getStatus());
        newValue.setValue(this.getValue());
        newValue.setUpdateTimeLong(this.getUpdateTimeLong());
        newValue.setUpdateTime(this.getUpdateTime());
        return newValue;
    }
}
